<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<script type="text/dialog">
    $('#input-image-1').ace_file_input({
        no_file:'请选择产品图片 ...',
        no_icon:'fa-file',
        btn_choose:'拖动图片或者点击选择图片',
        btn_change:'重新选择上传图片',
        droppable:true,
        onchange:null,
        thumbnail:false, //| true | large
        before_change:function(files,dropped){//设置允许上传的格式
            var allowed_files = [];
            for(var i = 0 ; i < files.length; i++) {
                var file = files[i];
                if(typeof file === "string") {
                    //IE8 and browsers that don't support File Object
                    if(! (/\.(gif|jpg|jpeg|png|bmp)$/i).test(file) ) return false;
                }
                else {
                    var type = $.trim(file.type);
                    if( !(/\.(gif|jpg|jpeg|png|bmp)$/i).test(file.name ))
                        continue;
                }
                allowed_files.push(file);
            }
            if(allowed_files.length == 0) return false;
            return allowed_files;
        }
    });
	$('#productEditForm').validate({
		errorElement: 'div',
		errorClass: 'help-block',
		focusInvalid: false,
		rules: {
			name:{
				required: true
			},
			cid:{
				required: true
			}
		},
		highlight: function (e) {
			$(e).closest('.form-group').removeClass('has-info').addClass('has-error');
		},
		success: function (e) {
			$(e).closest('.form-group').removeClass('has-error');//.addClass('has-info');
			$(e).remove();
		},submitHandler: function(form) {  
              $(form).ajaxSubmit({
					beforeSubmit: showRequest,
					success:       showResponse
			});  
        },  
		errorPlacement: function(error, element) {
			if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
		}
	});

function showRequest(formData, jqForm, options){
 	var queryString = $.param(formData);
 	art.dialog({id:'uploadDialog',title:'正在处理...',content:'<img src="../resources/admin/assets/images/loading2.gif">&nbsp;&nbsp;处理中，请稍后...',width:'250px',height:'120px',lock:true,background:'#ccc'});
 	return true;
 }
 function showResponse(data){
 	art.dialog.list['uploadDialog'].close();
 	//var data = eval('(' + data + ')');
 	if(data.success){
 		$(".error-text").html("");
		art.dialog({id:'editProductDialog'}).close();
		art.dialog({content:data.msg,resize:true,time:2,icon:'succeed',ok:function(){this.close()}});
		$('#productTable').DataTable().draw() ;		//重新刷新数据表格
 	}else{
 		$(".error-text").html(data.msg);
		return;
 	}
 }
</script>
<div class="scheduleBox">
	<h3 class="lighter block green">编辑产品</h3>
	<form class="form-horizontal" role= "form" id="productEditForm" name="productEditForm" method="post" action="${pageContext.request.contextPath}/product/product/productEdit" enctype="multipart/form-data">
        <input type="hidden" name="id" value="${product.id ne null ? product.id : 0}">
		<div class="form-group">
			<label class="col-sm-3 control-label no-padding-right"> 产品名称: </label>
			<div class="col-sm-8">
				<input type="text" id="name" class="col-sm-6" name="name" value="${product.name}"/>
			</div>
		</div>
		
		<div class="form-group">
			<label class="col-sm-3 control-label no-padding-right"> 产品描述: </label>
			<div class="col-sm-8 ">
				<input type="text"  class="col-sm-12"  name="title" value="${product.title}"/>
			</div>
		</div>
        <div class="form-group">
            <label class="col-sm-3 control-label no-padding-right"> 产品类别:${game.gcid} </label>
            <div class="col-sm-8">
                <c:forEach var="productCategory" items="${productCategory}">
                    <label>
                        <input name="cid" type="radio" class="ace" value="${productCategory.id}" ${product.cid eq productCategory.id ? "checked" : ""}/>
                        <span class="lbl"> ${productCategory.name}</span>
                    </label>
                </c:forEach>
            </div>
        </div>
        <c:if test="${not empty product.filepath}">
        <div class="form-group">
            <label class="col-sm-3 control-label no-padding-right"> 图标展示:</label>
            <div class="col-sm-4">
                <img src="../${product.filepath}" width="80%"/>
            </div>
        </div>
        </c:if>
        <div class="form-group">
            <label class="col-sm-3 control-label no-padding-right"> 产品图片:</label>
            <div class="col-sm-8">
                <input type="hidden" name="filepath" value="${product.filepath}">
                <input type="file" class="col-sm-4" name="imgFile"  id="input-image-1" accept="image/*">
            </div>
        </div>

		<div class="form-group">
			<div class="col-sm-4">
				<button type="submit" class="btn btn-sm btn-success">
				保存
				<i class="ace-icon fa fa-pencil bigger-110"></i>
				</button>
			</div>
		</div>
		
	</form>
</div>